#pragma once
#include"rbtree.h"

namespace bit
{
	template<class K , class V>
	class map
	{
		struct mapkeyoft
		{
			const K& operator ()(const pair<K,V> & kv)
			{
				return kv.first;
			}
		};
	public:
		typedef typename rbTree<K, pair<const K, V>, mapkeyoft>::Iterator iterator;
		typedef typename rbTree<K, pair<const K, V>, mapkeyoft>::const_Iterator const_iterator;

		iterator begin()
		{
			return _t.Begin();
		}
		iterator end()
		{
			return _t.End();
		}

		const_iterator begin() const
		{
			return _t.Begin();
		}

		const_iterator end() const
		{
			return _t.End();
		}

		iterator find(const K& key)
		{
			return _t.find(key);
		}

		pair<iterator,bool> insert(const pair<K, V>& kv)
		{
			return _t.insert(kv);
		}
		V& operator[](const K& key)
		{
			pair<iterator, bool> ret = insert(make_pair(key , V()));
			return ret.first->second;
		}
	private:
		rbTree<K, pair<const K,V>, mapkeyoft> _t;
	};

 

	void testmap()
	{
		map<int,int> t;
		int a[] = { 16,3,7,11,9,26,18,14,15 };
		int b[] = { 4,2,6,1,3,5,15,7,16,14 };
		for (auto e : b)
		{
			t.insert({e,e});
		}

		for (auto e : t)
		{
			cout << e.first<< " "<< e.second << endl;
		}
		cout << endl;
		
		t[6] = 99;

		map<int, int> ::iterator it = t.begin();
		while (it != t.end())
		{

			//it->first = 6;
			//it->second = 5;
			cout << it->first << "::" << it->second << endl;
			++it;
		}
		cout << endl;
		
	}
}